<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword"
                              placeholder="请输入姓名或电话"></el-input>
                </el-form-item>



                <el-form-item>
                    <el-button type="primary" v-on:click="getEmployees">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" min-width="55">
            </el-table-column>
            <el-table-column type="index" min-width="60">
            </el-table-column>
            <el-table-column prop="username" label="姓名" min-width="100" sortable>
            </el-table-column>
            <el-table-column prop="age" label="年龄" min-width="80" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" min-width="120" sortable>
            </el-table-column>
            <!--<el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
            </el-table-column>-->
            <el-table-column prop="state" label="状态" min-width="80" sortable>
                <template scope="scope">
                    <!-- 因为是装在scope里面的，所以用scope去取state属性 -->
                    <span size="small" style="color: green" v-if="scope.row.state == 1">在岗</span>
                    <span size="small" style="color: red" v-else-if="scope.row.state == 0">休息</span>
                    <span size="small" style="color: blue" v-else>出差</span>
                </template>
            </el-table-column>
            <el-table-column prop="phone" label="电话" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="password" label="密码" min-width="120" sortable>
            </el-table-column>
            <el-table-column label="操作" min-width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <!-- @click="handleDel(scope.$index, scope.row)" :是点击事件 参数为当前行的索引，以及当前行的数据 -->
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <!-- 分页 -->
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize"
                           :total="total"
                           style="float:right;">
            </el-pagination>

            <!--<el-pagination
            :page-size="pageSize"
            :pager-count="11"
            layout="prev, pager, next"
            :total="total"
            style="float:right;">
            </el-pagination>-->
        </el-col>
        <!-- 分页 -->



        <!--编辑界面-->
        <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="editForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="状态">
                    <el-radio-group v-model="editForm.state">
                        <el-radio class="radio" :label="1">在岗</el-radio>
                        <el-radio class="radio" :label="0">休息</el-radio>
                        <el-radio class="radio" :label="2">出差</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input v-model="editForm.password" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面，不用了  现在新增和编辑都走edit，这里只是舍不得删除，实际上已经没用了-->
        <!--<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="addForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <el-form-item label="生日">
                    <el-date-picker type="date" placeholder="选择日期" v-model="addForm.birth"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input type="textarea" v-model="addForm.addr"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>-->
    </section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    keyword: '',
                },
                options: [{
                    value: '选项1',
                    label: '黄金糕'
                }, {
                    value: '选项2',
                    label: '双皮奶'
                }],
                value: '',

                employees: [],
                total: 10,
                pageSize: 3,
                currentPage: 1,
                listLoading: false,
                sels: [], // 列表选中列
                title: "", // 显示是新增还是编辑

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    username: "",
                    phone: "",
                    state: 0,
                    email: "",
                    password: ""
                },

                /*addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
                addForm: {
					name: '',
					sex: -1,
					age: 0,
					birth: '',
					addr: ''
				}*/

            }
        },
        methods: {
            //部门显示转换
            /*formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },*/
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getEmployees();
            },
            //获取部门列表
            getEmployees() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true; // 忙等框

                // 发送请求
                this.$http.post("/employee/page",para)
                    .then(result=>{
                        this.employees = result.data.rows;
                        this.total = result.data.total;

                        this.listLoading = false; // 关闭忙等框
                    }).catch(result=>{
                    //console.debug(result)
                    alert("系统繁忙")
                })

                //NProgress.start();
                /*getDepartmentListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.departments = res.data.departments;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    //let para = { id: row.id };
                    //发送删除请求
                    this.$http.delete("/employee/"+row.id)
                        .then(result=>{ //请求发送成功
                            this.listLoading = false; // 关闭忙等框
                            if(result.data.success){ //删除成功

                                this.getEmployees();
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else { // 删除失败
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                            }
                            this.currentPage=1;
                            this.getEmployees();
                        }).catch(result=>{ //请求发送失败
                        this.$message({
                            message: '系统繁忙。',
                            type: 'error'
                        })

                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.editFormVisible = true;
                // 回显数据
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.editFormVisible = true;
                // 清空回显
                this. editForm = {
                    username: "",
                    phone: "",
                    state: 1,
                    email: "",
                    password: ""
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
                            // 编辑忙等框
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);

                            console.log(para);

                            // 转换数据格式用的，现在用不上
                            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            this.$http.post("/employee/save", para)
                                .then(reslut => {
                                    // 关闭编辑忙等框
                                    this.editLoading = false;
                                    if(reslut.data.success){
                                        this.$message({
                                            message: '操作成功',
                                            type: 'success'
                                        });
                                    } else {
                                        this.$message({
                                            message: reslut.data.message,
                                            type: 'error'
                                        });
                                    }
                                    // 关闭编辑模态框
                                    this.editFormVisible = false;
                                    // 调用函数，展示列表
                                    this.getEmployees();
                                }).catch(reslut => {
                                this.$message({
                                    message: '系统繁忙请稍后重试',
                                    type: 'error'
                                });
                            });

                            /*editDepartment(para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getDepartments();
                            });*/

                        }).catch(()=>{
                            alert('系统繁忙正在殴打程序员')
                        });
                    }
                });
            },
            //新增
            /*addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            addDepartment(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getDepartments();
                            });
                        });
                    }
                });
            },*/
            selsChange: function (sels) {
                this.sels = sels;
            },
            // 批量删除
            batchRemove: function () {
                // var ids = this.sels.map(item => item.id).toString(); // 这里是拿到id拼接成字符串
                var ids = this.sels.map(item => item.id) // 事实上我们不需要字符串，拿到数组就可以了
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };

                    // 向后台发送批量删除请求
                    this.$http.patch("/employee", ids)
                        .then(reslut => {
                            // 关闭忙等框
                            this.listLoading = false;
                            // 删除成功
                            if(reslut.data.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    message: reslut.data.message,
                                    type: 'error'
                                });
                            }
                        }).catch(reslut => {
                        this.$message({
                            message: '系统繁忙请稍后重试',
                            type: 'error'
                        });
                    })
                    // 删除成功后跳转到第一页去
                    this.currentPage = 1;
                    this.getEmployees()
                    /*batchRemoveDepartment(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getDepartments();
                    });*/
                }).catch(() => {
                    alert('取消删除成功')
                });
            },

        },
        mounted() {
            this.getEmployees();
        }
    }

</script>

<style scoped>

</style>
